<style lang="less">

</style>
<template>
    <div>
        <Drawer :closable="false" placement="right" v-model="drawerVisible" width="60%" :title="drawerTitle">
            <Row>
                <Col>
                    <Card :shadow="shadow">
                        <Row>
                            <Form ref="form" :model="form" :label-width="70" :rules="formValidate" style="position:relative">
                                <FormItem label="版本名称" prop="verName">
                                    <Input v-model="form.verName" style="width:600px"/>
                                </FormItem>
                                <FormItem label="版本号" prop="verNo">
                                    <Input v-model="form.verNo" style="width:600px"/>
                                </FormItem>
                                <FormItem label="版本标题" prop="verTitle">
                                    <Input v-model="form.verTitle" style="width:600px"/>
                                </FormItem>
                                <FormItem label="强制更新" prop="isFree" >
                                    <i-switch size="large" v-model="form.forceUpdate" @on-change="setForceUpdate" :true-value="1" :false-value="2">
                                        <span slot="open">强制</span>
                                        <span slot="close">非强制</span>
                                    </i-switch>
                                </FormItem>
                                <FormItem label="文件" prop="verUrl">
                                    <upload-file :fileUrl="form.verUrl" :fileSize="fileSize"  @on-change="handleUpload" width="400px" ref="upload"></upload-file>
                                </FormItem>
                                <FormItem label="课程介绍" prop="verContent">
                                    <div ref="editor"></div>
                                </FormItem>
                                <Form-item label class="br">
                                    <Button @click="editHTML" icon="ios-create-outline">编辑HTML代码</Button>
                                    <Button @click="fullscreenModal=true" icon="md-eye">预览</Button>
                                </Form-item>
                                <Form-item class="br">
                                    <Button type="primary" :loading="submitLoading" @click="handelSubmit" style="width:100px">提交</Button>
                                    <Button @click="handelCancel">取消</Button>
                                </Form-item>
                                <Spin size="large" fix v-if="loading"></Spin>
                            </Form>
                        </Row>
                    </Card>
                </Col>
            </Row>
            <!-- html编辑 -->
            <Modal title="编辑html代码" v-model="showHTMLModal" :mask-closable="false" :width="900" :fullscreen="full">
                <Input v-if="!full" v-model="dataEdit" :autosize="true" type="textarea" style="max-height:60vh;overflow:auto"/>
                <Input v-if="full" v-model="dataEdit" :autosize="true" type="textarea"/>
                <div slot="footer">
                    <Button @click="full=!full" icon="md-expand">全屏开/关</Button>
                    <Button @click="editHTMLOk" type="primary" icon="md-checkmark-circle-outline">确定保存</Button>
                </div>
            </Modal>
            <Modal title="预览" v-model="fullscreenModal" fullscreen>
                <div v-html="form.courseContent">{{form.courseContent}}</div>
                <div slot="footer">
                    <Button @click="fullscreenModal=false">关闭</Button>
                </div>
            </Modal>
        </Drawer>
    </div>
</template>
<script>
    import {uploadFile} from "@/api/index";
    import {getVersion, updateVersion} from "@/api/bus";
    import E from "wangeditor";
    // 表情包配置 自定义表情可在该js文件中统一修改
    import {sina} from "@/libs/emoji";
    import UploadFile from "../../my-components/flyme/upload-file";


    let editor = null;
    var xss = require("xss");
    export default {
        name: "versions-drawer",
        components: {
            UploadFile
        },
        data() {
            return {
                type: 0,
                loading: false, // 表单加载状态
                selectUsers: [],
                drawerTitle: "添加课程",
                drawerVisible: false,
                shadow: false,
                fileSize: 102400,
                form: {
                    verName: "",
                    verNo: "",
                    verTitle: "",
                    verUrl: "",
                    verContent: "",
                    forceUpdate: ""
                },
                formValidate: {
                    verName: [{required: true, message: "版本名称不能为空", trigger: "blur"}],
                    verNo: [{required: true, message: "版本号不能为空", trigger: "blur"}],
                    verTitle: [{required: true, message: "版本标题不能为空", trigger: "blur"}]
                },
                submitLoading: false, // 添加或编辑提交状态
                dataEdit: "", // 编辑数据
                showHTMLModal: false, // 显示html
                full: false, // html全屏开关
                fullscreenModal: false// 显示全屏预览
            };
        },
        methods: {
            init() {
                this.initEditor();
            },
            showDrawer(params) {
                if (params.id) {
                    this.type = 1;
                    this.drawerTitle = "编辑版本";
                    this.form.id = params.id;
                    this.getData();
                }
                this.drawerVisible = true;
            },
            //打开用户选择
            showUserChooseDrawer() {
                this.$refs.user.showDrawer();
            },
            clearUser() {
                this.$refs.user.clearSelectUser();
            },
            setForceUpdate(v){
                //免费赋值0,收费默认最低1
                this.form.forceUpdate=v;
            },
            initEditor() {
                let that = this;
                editor = new E(this.$refs.editor);
                editor.customConfig.onchange = html => {
                    this.form.verContent = html;
                };
                editor.customConfig.uploadImgServer = uploadFile;
                editor.customConfig.uploadImgHeaders = {
                    Authorization: that.getStore("accessToken")
                };
                editor.customConfig.uploadFileName = "file";
                editor.customConfig.uploadImgHooks = {
                    before: function (xhr, editor, files) {
                    },
                    success: function (xhr, editor, result) {
                    },
                    fail: function (xhr, editor, result) {
                        that.$Message.error("上传图片失败");
                    },
                    error: function (xhr, editor) {
                        that.$Message.error("上传图片出错");
                    },
                    timeout: function (xhr, editor) {
                        that.$Message.error("上传图片超时");
                    },
                    customInsert: function (insertImg, result, editor) {
                        if (result.status === true) {
                            let url = result.data.fileUrl;
                            insertImg(url);
                            that.$Message.success("上传图片成功");
                        } else {
                            that.$Message.error(result.info);
                        }
                    }
                };
                editor.customConfig.fontNames = ["微软雅黑", "宋体", "黑体", "Arial"];
                editor.customConfig.emotions = [
                    {
                        title: "新浪",
                        type: "image",
                        content: sina
                    }
                ];
                editor.create();
            },
            editHTML() {
                this.dataEdit = this.form.verContent;
                this.showHTMLModal = true;
            },
            editHTMLOk() {
                editor.txt.html(this.dataEdit);
                this.form.verContent = xss(this.dataEdit);
                this.showHTMLModal = false;
            },
            getData() {
                this.loading = true;
                console.info(JSON.stringify(this.form.id))
                getVersion(this.form.id).then(res => {
                    console.info(JSON.stringify(res.data))
                    this.loading = false;
                    if (res.status === true) {
                        this.form = res.data;
                        if (res.data.verContent == '<p><br></p>') {
                            this.form.verContent = "";
                        }
                        editor.txt.html(this.form.verContent);
                        this.$refs.upload.setPicUrl(this.form.verUrl);
                    }
                });
            },
            handelSubmit() {
                this.$refs.form.validate(valid => {
                    if (valid) {
                        this.submitLoading = true;
                        // 编辑
                        updateVersion(this.form).then(res => {
                            this.submitLoading = false;
                            if (res.status === true) {
                                this.$Message.success("操作成功");
                                this.$parent.getDataList();
                                this.drawerVisible = false;
                            }
                        });
                    }
                });
            },
            handleSelectUser(v) {
                this.form.lecturerId = v[0].id;
                this.selectUsers = v;
            },
            handelCancel() {
                this.drawerVisible = false;
            },
            handleUpload(fileUrl) {
                this.form.verUrl = fileUrl;
            }
        },
        mounted() {
            this.init();
        },
        watch: {}
    };
</script>